package site.yunnong.atvris.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.api.Page;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.dto.StarQueryParam;
import site.yunnong.atvris.common.enumeration.MediaTypeEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.common.utils.DateUtils;
import site.yunnong.atvris.dao.domain.UserStar;
import site.yunnong.atvris.dao.domain.UserStarExample;
import site.yunnong.atvris.dao.domain.Video;
import site.yunnong.atvris.dao.mapper.UserStarMapper;
import site.yunnong.atvris.service.InteractionService;
import site.yunnong.atvris.service.UserStarService;
import site.yunnong.atvris.service.VideoService;
import site.yunnong.atvris.service.vo.StarVO;
import site.yunnong.atvris.service.vo.VideoCardVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户收藏的视频
 *
 * @author zhangjinghao
 * @email jhzhang9102@163.com
 * @date 2021-07-09 23:45:37
 */
@Service
public class UserStarServiceImpl implements UserStarService {
    
    @Resource
    private UserStarMapper userStarMapper;
    @Resource
    private VideoService videoService;
    @Resource
    private InteractionService interactionService;

    @Override
    public int findMediaStarStatus(Long uuid, Integer mediaType, Long uvid) {
        UserStarExample example = new UserStarExample();
        example.createCriteria().andUuidEqualTo(uuid)
                .andMediaIdEqualTo(uvid)
                .andMediaTypeEqualTo(mediaType);
        return (int) userStarMapper.countByExample(example);
    }

    @Override
    public void addStar(UserStar userStar) {
        userStarMapper.insertSelective(userStar);
    }

    @Override
    public void delStar(UserStar userStar) {
        if (userStar.getId() != null) {
            userStarMapper.deleteByPrimaryKey(userStar.getId());
            return;
        }
        UserStarExample example = new UserStarExample();
        UserStarExample.Criteria criteria = example.createCriteria();
        if (userStar.getUuid() == null) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_BUSINESS, "数据异常");
        } else {
            criteria.andUuidEqualTo(userStar.getUuid());
        }
        if (userStar.getPackId() != null) {
            criteria.andPackIdEqualTo(userStar.getPackId());
        }
        if (userStar.getMediaId() != null) {
            criteria.andMediaIdEqualTo(userStar.getMediaId());
        }
        if (userStar.getMediaType() != null) {
            criteria.andMediaTypeEqualTo(userStar.getMediaType());
        }
        userStarMapper.deleteByExample(example);
    }

    @Override
    public Page<StarVO> list(StarQueryParam param) {
        UserStarExample example = new UserStarExample();
        example.createCriteria().andUuidEqualTo(param.getUuid())
                .andMediaTypeEqualTo(param.getMediaType());
        com.github.pagehelper.Page<UserStar> starPage = PageHelper.startPage(param.getPageNum(), param.getPageSize()).doSelectPage(() -> {
            userStarMapper.selectByExample(example);
        });
        List<StarVO> starVOList = Optional.of(starPage.getResult()).orElse(Lists.newArrayList())
                .stream().map(this::buildStarVO).collect(Collectors.toList());
        
        return new Page<>(starPage.getPageNum(), starPage.getPageSize(), starPage.getTotal(), starVOList);
    }

    @Override
    public int deleteById(Long id) {
        return userStarMapper.deleteByPrimaryKey(id);
    }

    private StarVO buildStarVO(UserStar star) {
        // todo 暂时只支持视频
        StarVO vo = new StarVO();
        vo.setId(star.getId());
        vo.setCreateTime(star.getCreateTime());
        // 获取视频信息
        Long mediaId = star.getMediaId();
        Video video = videoService.queryApprovedVideoByUvid(mediaId);
        if (video == null) {
            // 视频丢失
            return vo;
        }
        VideoCardVO videoCardVO = new VideoCardVO();
        BeanUtils.copyProperties(video, videoCardVO);
        videoCardVO.setDuration(DateUtils.covertSecondToDuration(video.getVideoTime()));
        videoCardVO.setViewCount(interactionService.findMediaViewCount(MediaTypeEnum.VIDEO.getValue(), mediaId));
        videoCardVO.setLikedCount(interactionService.findMediaLikedCount(MediaTypeEnum.VIDEO.getValue(), mediaId));
        vo.setVideoCardVO(videoCardVO);
        return vo;
    }
}

